cc.Class({
    extends: cc.Component,

    properties: {
        item: cc.Node,
        content: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    onEnable() {
        this.lobbyMain = cc.find("Canvas").getComponent("lobbyMain");
        this.lobbyNet = this.lobbyMain.lobbyNet;
        this.playerInfo = this.lobbyMain.playerInfo;
        this.configJson = this.lobbyMain.configJson;

        this.lobbyMain.openLoading();
        this.imgArr = [];
        this.eventData = null;
        this.panelName = null;
    },

    start() {
        this.right = this.node.getChildByName("bg").getChildByName("right");
        this.mask = this.right.getChildByName("mask");
        this.getEventData();
    },

    onClick(event, target) {
        this.lobbyMain.playClickSound();
        if (target == "joinNow") {  //参加活动 界面跳转
            this.openPanel(this.panelName);
        } else if (target == "close") {
            this.lobbyMain.hidePanel(this.node);
        } else if (target == "eventId") {
            this.panelName = event.target.panelName;
            this.showEvent(event.target.eventId);  //显示事件
        }
    },


    //界面跳转
    openPanel(panelName) {
        this.lobbyMain.onClick(null, panelName);
    },

    // 显示事件
    showEvent(eventId) {
        const event = this.eventData.find(e => e.id == eventId);
        const imgObj = this.imgArr.find(img => img.id == eventId);

        if (event && imgObj && imgObj.img) {
            const sprite = this.mask.getChildByName("img").getComponent(cc.Sprite);
            if (sprite) {
                sprite.spriteFrame = imgObj.img;
            }
            this.right.getChildByName("btn_shop").active = event.isButton;
        }
    },


    //获取事件信息
    getEventData() {
        let data = {
            "action": "getEventData",
        }
        this.lobbyNet.sendMessage(data);
    },

    //接收事件信息
    onEventData(data) {
        // this.lobbyMain.loading.active = false;
        if (data.respCode == "SUCCESS") {
            let info = data.data;
            this.eventData = info;
            let isFist = true;
            for (let i = 0; i < info.length; i++) {
                const elem = info[i];
                if (elem.isShow) {
                    this.loadEventImg(elem, isFist);
                    isFist = false;
                }
            }
            // this.setEventItem(info);
        } else {
            this.lobbyMain.showTips("Failed to get event information");
        }
    },


    //加载活动图片
    loadEventImg(data, isFist) {
        console.log("loadEventImg");
        let serverIp = this.configJson.serverIp;
        let port_lobby = this.configJson.port_lobby;
        const path = "http://" + serverIp + ":" + port_lobby + "/" + data.img.replace(/\\/g, "/");
        let self = this;
        cc.assetManager.loadRemote(path, (err, texture) => {
            if (err) {
                console.error("加载远程图片失败：", err);
                return;
            }
            let obj = {
                id: data.id,
                img: new cc.SpriteFrame(texture)
            }
            self.imgArr.push(obj);
            if (isFist) {
                self.setFirst(obj.img);
                self.right.getChildByName("btn_shop").active = data.isButton;
                self.panelName = data.panelName;
            }
            //添加活动按钮
            let node = cc.instantiate(self.item);
            node.getChildByName("label").getComponent(cc.Label).string = data.name;
            node.eventId = data.id;
            node.panelName = data.panelName;
            this.content.addChild(node);
        });
    },

    // 设置第一个
    setFirst(img) {
        this.mask.getChildByName("img").getComponent(cc.Sprite).spriteFrame = img;
        this.mask.getChildByName("label").active = false;
        this.lobbyMain.loading.active = false;
    },
});
